<?php

defined("IN_CART") || die;

//应用
class CAPP extends CBase {
	
	
	private static $c;

	private static $a;

	//初始化应用
	public static function init() {
		date_default_timezone_set("Asia/Shanghai");
		
		//定义系统变量
		define("C_TIME",	 time());	
		define('C_REQMETHOD',isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : '');
		define('C_ISGET',	 C_REQMETHOD == 'GET' );
		define('C_ISPOST',	 C_REQMETHOD == 'POST');
		define('C_ISAJAX',	 (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) == 'XMLHTTPREQUEST') || !empty($_POST['isajax']) || !empty($_GET['isajax']));

		
		@ini_set("memory_limit",'64M');
		@ini_set('session.cache_expire',  180);
		@ini_set('session.use_trans_sid', 0);
		@ini_set('session.use_cookies',   1);
		@ini_set('session.auto_start',    0);

		//初始化session
		Session::getInstance()->init();
		//转义
		if(get_magic_quotes_gpc()) {
			!empty($_GET) && $_GET  = cstripslashes($_GET);
			!empty($_GET) && $_POST = cstripslashes($_POST); 
		}
	}
	
	public static function getControllerName() {
		return self::$c;
	}

	public static function getActionName() {
		return self::$a;
	}

	//执行app
	public static function run() {
		self::init();

		static $_app = array();

		$params	= Router::getRequest();

		self::$c = $c		= $params["c"];
		self::$a = $a		= $params["a"];
		$appId  = $c . '_' . $a;
		if(!isset($_app[$appId])) {
			C("TEMPLATE_NAME",$c . '/' . $a);
			$c = ucfirst($c . "Controller");
			$a .= "Action";
			if(is_file(CPATH_CONTROLLER. '/' . $c.'.class.php')) {
				CFrame::loadFile(CPATH_CONTROLLER.'/'.$c.'.class.php');
			} else if(is_file(CPATH_COMMON . '/'.$c.'.class.php')) {
				CFrame::loadFile(CPATH_COMMON.'/'.$c.'.class.php');
			}
		} 

		$object = new $c();
		if(method_exists($object,$a)) { //如果c中存在a方法，执行，
			$_app[$appId] = $object->$a();
		} else {	//如果不存在，404报错
			self::display404();
		}
		return $_app[$appId];

	}

	
	
	
}